package com.jspgou.cms.action.store;

import static com.jspgou.cms.Constants.STORE_SYS;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.jspgou.cms.entity.AftersaleType;
import com.jspgou.cms.entity.Role;
import com.jspgou.cms.entity.ShopMember;
import com.jspgou.cms.entity.Store;
import com.jspgou.cms.entity.StoreCategory;
import com.jspgou.cms.manager.AftersaleTypeMng;
import com.jspgou.cms.manager.StoreCategoryMng;
import com.jspgou.cms.web.CmsUtils;
import com.jspgou.cms.web.ShopFrontHelper;
import com.jspgou.cms.web.SiteUtils;
import com.jspgou.common.web.springmvc.MessageResolver;
import com.jspgou.core.entity.Website;

@Controller
public class StoreCategoryAct {
	/**
	 * 店铺类目列表页
	 */
	private static final String TPL_STORE_CATEGORY_LIST="tpl.storeCategoryList";
	/**
	 * 店铺类目添加页
	 */
	private static final String TPL_STORE_CATEGORY_ADD="tpl.storeCategoryAdd";
	/**
	 * 店铺类目修改页
	 */
	private static final String TPL_STORE_CATEGORY_EDIT="tpl.storeCategoryEdit";
	
	// 店铺类目列表
	@RequestMapping(value = "/storeCategory/list.jspx")
	public String list(String cl,Integer pageNo,HttpServletRequest request,
			ModelMap model) {
		Website web = SiteUtils.getWeb(request);
		ShopMember member=CmsUtils.getMember(request);
		//用户没有登录，跳转到登录页
		if(member == null){
			return ShopFrontHelper.showLogin(request, model, web);
		}
		Store store=member.getStore();
		model.addAttribute("store", store);
		model.addAttribute("cl", cl);
		ShopFrontHelper.setCommonData(request, model, web);
		ShopFrontHelper.frontPageData(request, model);
		return web.getTplSys(STORE_SYS, MessageResolver.getMessage(request,TPL_STORE_CATEGORY_LIST), request);
	}
	
	// 店铺类目添加
	@RequestMapping(value = "/storeCategory/add.jspx")
	public String add(Long id,
			HttpServletRequest request,ModelMap model) {
		Website web = SiteUtils.getWeb(request);
		ShopMember member=CmsUtils.getMember(request);
		//用户没有登录，跳转到登录页
		if(member == null){
			return ShopFrontHelper.showLogin(request, model, web);
		}
		Store store=member.getStore();
		model.addAttribute("store", store);
		ShopFrontHelper.setCommonData(request, model, web);
		ShopFrontHelper.frontPageData(request, model);
		return web.getTplSys(STORE_SYS, MessageResolver.getMessage(request,TPL_STORE_CATEGORY_ADD), request);
	}
	
	// 店铺类目保存
	@RequestMapping(value = "/storeCategory/save.jspx")
	public String save(StoreCategory bean,Long parentId,
			HttpServletRequest request,ModelMap model) {
		Website web = SiteUtils.getWeb(request);
		ShopMember member=CmsUtils.getMember(request);
		//用户没有登录，跳转到登录页
		if(member == null){
			return ShopFrontHelper.showLogin(request, model, web);
		}
		bean.setStore(member.getStore());
		bean.setWebsite(web);
		if(parentId!=null){
			bean.setParent(manager.findById(parentId));
		}
		manager.save(bean);
		ShopFrontHelper.setCommonData(request, model, web);
		ShopFrontHelper.frontPageData(request, model);
		return "redirect:list.jspx";
	}
	
	// 店铺类目修改
	@RequestMapping(value = "/storeCategory/edit.jspx")
	public String edit(Long id,
			HttpServletRequest request,ModelMap model) {
		Website web = SiteUtils.getWeb(request);
		ShopMember member=CmsUtils.getMember(request);
		//用户没有登录，跳转到登录页
		if(member == null){
			return ShopFrontHelper.showLogin(request, model, web);
		}
		Store store=member.getStore();
		model.addAttribute("store", store);
		StoreCategory bean=manager.findById(id);
		model.addAttribute("bean", bean);
		ShopFrontHelper.setCommonData(request, model, web);
		ShopFrontHelper.frontPageData(request, model);
		return web.getTplSys(STORE_SYS, MessageResolver.getMessage(request,TPL_STORE_CATEGORY_EDIT), request);
	}
	
	// 店铺类目更新
	@RequestMapping(value = "/storeCategory/update.jspx", method = RequestMethod.POST)
	public String update(Long id,HttpServletRequest request,ModelMap model) {
		Website web = SiteUtils.getWeb(request);
		ShopMember member=CmsUtils.getMember(request);
		//用户没有登录，跳转到登录页
		if(member == null){
			return ShopFrontHelper.showLogin(request, model, web);
		}
		StoreCategory bean=manager.findById(id);
		manager.update(bean);
		return "redirect:list.jspx";
	}
	
	
	//店铺类目删除
	@RequestMapping("/storeCategory/delete.jspx")
	public String Delete(Long[] ids, Integer pageNo, HttpServletRequest request,
			ModelMap model) {
		manager.deleteByIds(ids);
		return "redirect:list.jspx";
	}
	
	@Autowired
	private StoreCategoryMng manager;
	@Autowired
	private AftersaleTypeMng aftersaleTypeMng;
}
